<?php
namespace backend\controllers;

use Yii;
use yii\filters\VerbFilter;
use yii\web\Response;
/**
 * Base controller
 */
abstract class BaseController extends \common\bases\BaseController
{
	/**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * 设置页面返回方式
     */
    public function setResponseFormat($format='html'){
        switch ($format) {
            case 'json':
                Yii::$app->response->format = Response::FORMAT_JSON;
                break;
            case 'jsonp':
                Yii::$app->response->format = Response::FORMAT_JSONP;
                break;
            case 'html':
                Yii::$app->response->format = Response::FORMAT_HTML;     
                break;
            case 'raw':
                Yii::$app->response->format = Response::FORMAT_RAW;        
                break;
            case 'xml':
                Yii::$app->response->format = Response::FORMAT_XML;        
                break;
            default:
                Yii::$app->response->format = Response::FORMAT_HTML;
                break;
        }
    }

    /**
     * 处理 model 插入报错的文本信息
     * @params errors array(2) { 
            ["created_at"]=> array(1) { [0]=> string(27) "创建时间不能为空。",
            ["updated_at"]=> array(1) { [0]=> string(27) "更新时间不能为空。" }
        }
     */
    public function getErrorText($errors){
        $error_text = "";
        $error_arr  = [];
        if (is_array($errors) && count($errors) > 0) {
            foreach ($errors as $items) {
                foreach ($items as $row) {
                    $error_arr[] = $row;
                }
            }
            $error_text = join("",$error_arr);
        }

        return $error_text;
    }
    
    /**
     * 设置消息
     */
    public function setSuccess($message){
        Yii::$app->getSession()->setFlash('success',$message);
    }
    
    /**
     * 设置消息
     */
    public function setError($message){
        Yii::$app->getSession()->setFlash('error',$message);
    }

    public function getTime($time){
        return date("Y-m-d H:i:s",$time);
    }
}